﻿using FRMS.BUS;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class UserController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public UserController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
          )
        {
            JwtSettings jwtSettings = new JwtSettings();
            jwtSettings.Audience = AppSettingsHelper.Configuration["JwtSettings:Audience"];
            jwtSettings.Issuer = AppSettingsHelper.Configuration["JwtSettings:Issuer"];
            jwtSettings.SecretKey = AppSettingsHelper.Configuration["JwtSettings:SecretKey"];
            _jwtSettings = jwtSettings;
            //_jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
        }

        /// <summary>
        /// 新建用户
        /// </summary>
        [HttpPost]
        [Route("AddUser")]
        public ResponseMessage AddUser(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserBUS BUS = new UserBUS();
            try
            {
                userInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.AddUser(userInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "新增用户";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "新增用户异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        [HttpPost]
        [Route("UserEdit")]
        public ResponseMessage UserEdit(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserBUS BUS = new UserBUS();
            try
            {
                message = BUS.EditUser(userInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "修改用户";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "修改用户异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        [HttpPost]
        [Route("UserDelete")]
        public ResponseMessage UserDelete(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserBUS BUS = new UserBUS();
            try
            {
                message = BUS.DeleteUser(userInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除用户";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除用户异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询用户
        /// </summary>
        [HttpPost]
        [Route("QueryUser")]
        public ResponseMessage QueryUser(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserBUS BUS = new UserBUS();
            try
            {
                userInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryUser(userInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询用户";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询用户异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 载入用户信息
        /// </summary>
        [HttpPost]
        [Route("LoadUser")]
        public ResponseMessage LoadUser(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserBUS BUS = new UserBUS();
            try
            {
                message = BUS.LoadUser(userInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "载入用户";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "载入用户信息异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        [HttpPost]
        [Route("ModifyPwd")]
        public ResponseMessage ModifyPwd(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserBUS BUS = new UserBUS();
            try
            {
                message = BUS.ModifyPwd(userInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "重置密码";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "重置密码异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询菜单下所有功能按钮
        /// </summary>
        [HttpPost]
        [Route("QueryMenuFoption")]
        public ResponseMessage QueryMenuFoption()
        {
            ResponseMessage message = new ResponseMessage();
            UserBUS BUS = new UserBUS();
            try
            {
                string result = _access.HttpContext.Session.GetString("ProjectID").ToString();
                int id = int.Parse(result);
                message = BUS.GetFoption(id);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询项目功能按钮";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询项目功能按钮异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }
    }
}
